﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class NoiPhatHanhBUS
    {
        public void themNoiPhatHanh(NoiPhatHanh nptDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.NoiPhatHanhs.InsertOnSubmit(nptDTO);

            storeDB.SubmitChanges();
        }

        public void suaNoiPhatHanh(NoiPhatHanh nptDTO, string maNoiPhatHanh)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiPhatHanhs.Where(q => q.MaNoiPhatHanh == maNoiPhatHanh).First();
            query.TenNoiPhatHanh = nptDTO.TenNoiPhatHanh;

            storeDB.SubmitChanges();
        }

        public void xoaNoiPhatHanh(GridView gridView)
        {
            //Lay MaNoiPhatHanh duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaNoiPhatHanh"));

                if (_checkBox.Checked == true)
                {
                    string maNoiPhatHanhCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maNoiPhatHanhCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaNoiPhatHanh duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.NoiPhatHanhs.Where(q => q.MaNoiPhatHanh == maTemp).First();

                storeDB.NoiPhatHanhs.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachNoiPhatHanh();
            gridView.DataBind();
        }

        public IList<NoiPhatHanh> layDanhSachNoiPhatHanh()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstNoiPhatHanh = storeDB.NoiPhatHanhs.ToList();

            return lstNoiPhatHanh;
        }

        public Boolean kiemtraTonTaiMaNoiPhatHanh(string maNoiPhatHanh)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiPhatHanhs.Where(q => q.MaNoiPhatHanh == maNoiPhatHanh).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenNoiPhatHanh(string tenNoiPhatHanh)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiPhatHanhs.Where(q => q.TenNoiPhatHanh == tenNoiPhatHanh).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaNoiPhatHanh(string maNoiPhatHanh)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maNoiPhatHanh = storeDB.NoiPhatHanhs.Where(m => m.MaNoiPhatHanh == maNoiPhatHanh).Select(m => m.MaNoiPhatHanh).First();

            return _maNoiPhatHanh;
        }

        public string layTenNoiPhatHanh(string maNoiPhatHanh)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenNoiPhatHanh = storeDB.NoiPhatHanhs.Where(m => m.MaNoiPhatHanh == maNoiPhatHanh).Select(m => m.TenNoiPhatHanh).First();

            return tenNoiPhatHanh;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiPhatHanhs.ToList();
            foreach (var q in query)
            {
                var maNoiPhatHanh = q.MaNoiPhatHanh;
                var tenNoiPhatHanh = q.TenNoiPhatHanh;

                ListItem item = new ListItem();
                item.Text = tenNoiPhatHanh;
                item.Value = maNoiPhatHanh;

                dropDownList.Items.Add(item);
            }
        }

        public IList<NoiPhatHanh> layThongTinNoiPhatHanhTheoMaNoiPhatHanh(NoiPhatHanh nphDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiPhatHanhs.Where(q => q.MaNoiPhatHanh == nphDTO.MaNoiPhatHanh).ToList();

            return query;
        }
    }
}
